package example.oauth.ui;

import java.util.Map;

import example.oauth.OAuth;
import example.service.TokenService;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class OAuthAccessActivity extends Activity {
	@Override
	protected void onNewIntent(Intent intent) {
    	Uri uri = intent.getData();
    	if(null!=uri){
    		String url = uri.toString();
    		Log.i(TAG, "Activity_URL："+url);
    		int index = url.lastIndexOf('=');
    		if(-1!=index){
    			txtVerifier.setText(url.substring(index+1));
    		}
    	}
    	
		super.onNewIntent(intent);
		
	}

	/** Called when the activity is first created. */
	
	public static final String TAG = "OAuthAccessActivity";
	private String requestToken;
	private String tokenSecret;
	private EditText txtVerifier;
	private Button btnRequestToken;
	private Button btnAccessToken;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.oauth_access_activity);
        btnRequestToken = (Button)findViewById(R.id.btnRequestToken);
        btnAccessToken = (Button)findViewById(R.id.btnAccessToken);
        txtVerifier = (EditText)findViewById(R.id.txtVerifier);
        
        btnRequestToken.setOnClickListener(btnListener);
        btnAccessToken.setOnClickListener(btnListener);
    }
    
    private OnClickListener btnListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()){
			case R.id.btnRequestToken:
				OAuthAccessActivity.this.getRequestToken();
				break;
			case R.id.btnAccessToken:
				OAuthAccessActivity.this.getVerifier();
				break;
			}
			
		}
	}; 
	protected void testURL(){
		String url = "http://192.168.1.2:8080/index2.html";
		Uri uri = Uri.parse(url);
		Intent intent = new Intent(Intent.ACTION_VIEW,uri);
		startActivity(intent);
	}
    //442514
    protected void getRequestToken(){
    	Map<String,String> params = TokenService.getRequestToken();
    	requestToken = params.get(OAuth.OAUTH_TOKEN);
    	tokenSecret = params.get(OAuth.OAUTH_TOKEN_SECRET);
    	if(null == requestToken){
    		Log.i(TAG, "requestToken为空");
    	}else{
    		Log.i(TAG, "requestToken："+requestToken);
    		String url = "https://open.t.qq.com/cgi-bin/authorize?oauth_token="+requestToken;
    		Uri uri = Uri.parse(url);
    		Intent intent = new Intent(Intent.ACTION_VIEW,uri);
    		startActivity(intent);
    	}

    }
	protected void getVerifier(){
		
		String verifier = txtVerifier.getText().toString().trim();
		Log.i(TAG, "验证码："+verifier);
		Log.i(TAG, "requestToken："+requestToken);
		Log.i(TAG, "tokenSecret："+tokenSecret);
		//String url = "https://open.t.qq.com/cgi-bin/access_token?"+TokenService.getAccessToken(requestToken,tokenSecret,verifier);
		String results = TokenService.getAccessToken(requestToken,tokenSecret,verifier);
		Log.i(TAG, "返回值："+results);
	}
    
    
    
    
    
    
    
}